package com.jackson.dao.Impl;

import com.jackson.dao.AccountUser;
import com.jackson.domain.Account;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.SQLException;
import java.util.List;

/*
 * Author: JacksonCoder
 * FileName: UserDao
 * Version: 1.0.0
 */

@Repository   //相当与配饰了 bean 标签
public class AccountDao implements AccountUser {

    @Autowired
    private QueryRunner queryRunner;


    @Override
    public List<Account> findAll() {
        List<Account> list = null;
        // sql
        String sql = "select * from account";
        try {
            list = queryRunner.query(sql, new BeanListHandler<Account>(Account.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return list;
    }

    @Override
    public Account findById(Integer id) {
        Account query = null;
        // sql
        String sql = "select * from account where id = ?";
        try {
            query = queryRunner.query(sql, new BeanHandler<Account>(Account.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return query;
    }

    @Override
    public void save(Account account) {
        String sql = "insert into account values(null,?,?)";
        try {
            queryRunner.update(sql, account.getName(), account.getMoney());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(Account account) {
        String sql = "update account set name = ?,money = ? where id = ?";
        try {
            queryRunner.update(sql, account.getName(), account.getMoney(), account.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(Integer id) {
        String sql = "delete from account where id = ?";
        try {
            queryRunner.update(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
